'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _request = require('../_util/request');

var _request2 = _interopRequireDefault(_request);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

/**
 * 数据字典组件
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
var Dict = function (_React$Component) {
    (0, _inherits3['default'])(Dict, _React$Component);

    function Dict() {
        (0, _classCallCheck3['default'])(this, Dict);
        return (0, _possibleConstructorReturn3['default'])(this, (Dict.__proto__ || Object.getPrototypeOf(Dict)).apply(this, arguments));
    }

    return Dict;
}(React.Component);
// 配置


exports['default'] = Dict;
Dict.url = '';
// 获取数据字典
Dict.getDicts = function (dictKeys) {
    var url = Dict.url;

    var dicts = {};
    // sessionStorage中有缓存直接取，没有就从服务器查询
    var needQueryDictKeys = [];
    dictKeys.forEach(function (dictkey) {
        var exist = sessionStorage['dict_' + dictkey];
        if (exist) {
            dicts[dictkey] = JSON.parse(exist);
        } else {
            needQueryDictKeys.push(dictkey);
        }
    });
    if (needQueryDictKeys.length > 0) {
        return (0, _request2['default'])(url, {
            method: 'POST',
            body: { dictKeys: needQueryDictKeys }
        }).then(function (resp) {
            if (resp && resp.dicts) {
                dicts = (0, _extends3['default'])((0, _extends3['default'])({}, dicts), resp.dicts);
                // 缓存
                for (var dictkey in resp.dicts) {
                    if (resp.dicts.hasOwnProperty(dictkey)) {
                        sessionStorage['dict_' + dictkey] = JSON.stringify(resp.dicts[dictkey]);
                    }
                }
            }
            return Promise.resolve(dicts);
        });
    } else {
        return Promise.resolve(dicts);
    }
};
module.exports = exports['default'];